﻿using System;
using System.Net;
using System.Web.Mvc;
using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Services.Configuration;
using Nop.Web.Models.WindAlert;
using Nop.Core.Domain.WindAlert;
using Nop.Services.WindAlert;


namespace Nop.Web.Controllers
{
    public partial class WForecastController : BasePublicController
    {
         #region Fields

        private readonly IWorkContext _workContext;
        private readonly ISettingService _settingService;
        private readonly WindAlertSettings _windAlertSettings;
        private readonly IWindHelpers _windHelper;

        #endregion Fields

        #region Constructors

        public WForecastController(IWorkContext workContext, ISettingService settingService, WindAlertSettings windAlertSettings, IWindHelpers windHelper)
        {
            this._settingService = settingService;
            this._workContext = workContext;
            this._windAlertSettings = windAlertSettings;
            this._windHelper = windHelper;
        }

        #endregion
        
        #region Utilities

        [NonAction]
        protected bool IsCurrentUserRegistered()
        {
            return _workContext.CurrentCustomer.IsRegistered();
        }

        [NonAction]
        protected string getNopfinderLink(int hour, int AddDays)
        {
            string returnStr = "";
            string windHour = "";
            switch (hour)
            {
                case 0:
                    windHour = "06"; // 9:00
                    if (AddDays == 1)
                        windHour = "30";
                    if (AddDays == 2)
                        windHour = "54";
                    if (AddDays == 3)
                        windHour = "78";
                    break;
                case 1:
                    windHour = "09"; // 12:00
                    if (AddDays == 1)
                        windHour = "33";
                    if (AddDays == 2)
                        windHour = "57";
                    if (AddDays == 3)
                        windHour = "81";
                    break;
                case 2:
                    windHour = "12"; // 15:00
                    if (AddDays == 1)
                        windHour = "36";
                    if (AddDays == 2)
                        windHour = "60";
                    if (AddDays == 3)
                        windHour = "84";
                    break;
                case 3:
                    windHour = "15"; //18:00
                    if (AddDays == 1)
                        windHour = "39";
                    if (AddDays == 2)
                        windHour = "63";
                    if (AddDays == 3)
                        windHour = "87";
                    break;
                case 4:
                    windHour = "18"; // 21:00
                    if (AddDays == 1)
                        windHour = "21";
                    if (AddDays == 2)
                        windHour = "66";
                    if (AddDays == 3)
                        windHour = "90";
                    break;
            }
            returnStr = "http://www.windfinder.com/grafiken/forecasts/wind_israel";
            returnStr += windHour;
            returnStr += ".png";
            return returnStr;
        }

        [NonAction]
        private string GetWeatheronline(string hour, int addDays)
        {
            //                                                    forecast date   /   time /creation date 
            //http://www.weatheronline.co.uk/daten/sailcharts/gfs/2014/01/30/uv10_g834_00-2014013000.gif
          
            string returnStr = "http://www.weatheronline.co.uk/daten/sailcharts/gfs/";
            returnStr += DateTime.UtcNow.AddDays(addDays).ToString("yyyy/MM/dd") + "/uv10_g834_" + hour;
            returnStr += "-" + DateTime.UtcNow.ToString("yyyyMMdd") + "00.gif";
            return returnStr;
        }

        [NonAction]
        private string GetUOA(int hour, int addDays)
        {
            string returnStr = "";
            string uohHour = "";
            //http://forecast.uoa.gr/LINKS/SKIRON/georain/024.gif
            switch (hour)
            {
                case 0:
                    uohHour = "018";
                    if (addDays == 1)
                        uohHour = "042";
                    if (addDays == 2)
                        uohHour = "066";
                    if (addDays == 3)
                        uohHour = "090";
                    break;
                case 2:
                    uohHour = "024";
                    if (addDays == 1)
                        uohHour = "048";
                    if (addDays == 2)
                        uohHour = "072";
                    if (addDays == 3)
                        uohHour = "096";
                    break;
                case 4:
                    uohHour = "030";
                    if (addDays == 1)
                        uohHour = "054";
                    if (addDays == 2)
                        uohHour = "078";
                    if (addDays == 3)
                        uohHour = "102";
                    break;
                default:
                    return returnStr;
            }

            // build greece url
            returnStr = "http://forecast.uoa.gr/LINKS/SKIRON/wind10m/";
            returnStr += uohHour;
            returnStr += ".gif";
            return returnStr;

        }

        [NonAction]
        public string buildGraph(double x , double y)
        {
            //http://isramar.ocean.org.il/isramar2009/wave_model/ShowTimeSeries.aspx?x=34.9267&y=32.4615&model=wam&modeldate=0911290000&region=fine
            string strUrl = "http://isramar.ocean.org.il/isramar2009/wave_model/ShowTimeSeries.aspx?x=" + x + "&y=" + y + "&model=wam&modeldate=";
            strUrl += DateTime.Now.ToString("yyMMdd") + "0000" + "&region=fine";

            //string alltext = "";
            string strResults = _windHelper.GetPageData(strUrl);
            //Response.Write(strResults);

            string[] sep = new string[] { "<img id=\"Chart_Image\" src=\"" };
            string[] arrTemp = strResults.Split(sep, System.StringSplitOptions.RemoveEmptyEntries);
            //Response.Write(arrTemp[1]);
            sep = new string[] { "\"" };
            arrTemp = arrTemp[1].Split(sep, System.StringSplitOptions.RemoveEmptyEntries);
            //Response.Write(arrTemp[0]);

            return "http://isramar.ocean.org.il/isramar2009/wave_model/" + arrTemp[0];
        }

        [NonAction]
        private string GetIsramarWaves(int updayeHoursAgo, string hour, int addDays)
        {
            //                                                                  creation date   / forecast date 
            //http://isramar.ocean.org.il/isramar2009/wave_model/wave_maps/swan/1401291200/israel/14013012.windir.gif
            updayeHoursAgo = (-1) * updayeHoursAgo;
            string returnStr = "http://isramar.ocean.org.il/isramar2009/wave_model/wave_maps/swan/";
            returnStr += DateTime.UtcNow.AddHours(updayeHoursAgo).ToString("yyMMdd") + "1200/israel/";
            returnStr += DateTime.UtcNow.AddDays(addDays).ToString("yyMMdd") + hour + ".windir.gif";
            return returnStr;
        }

         [NonAction]
        private int CheckUpadteDate()
        {
            int hoursAgo = 36;
            string imageString = GetIsramarWaves(12,"06",0);
            string strResults = _windHelper.GetPageData(imageString);
            if (strResults  != null)
                return 12;

            imageString = GetIsramarWaves(24, "06", 0);
            strResults = _windHelper.GetPageData(imageString);
            if (strResults != null)
                return 24;

            imageString = GetIsramarWaves(36, "06", 0);
            strResults = _windHelper.GetPageData(imageString);
            if (strResults != null)
                return 36;
            return hoursAgo;
        }
        #endregion

        public ActionResult Index(int? addDays)
        {
            if (addDays == null)
                addDays = 0;
            var model = new ForecastModel();

            if (!IsCurrentUserRegistered())
                model.IsRegistered = false;
            else
                model.IsRegistered = true;

            model.addDays = (int)addDays;
            model.BlockForecast = _windAlertSettings.BlockForecast;
            return View(model);
        }

        public ActionResult WindMaps(int addDays)
        {
            var forecate = new ForecastMapsModel();
            forecate.WindFinder8 = getNopfinderLink(0, addDays);
            forecate.WindFinder11 = getNopfinderLink(1, addDays);
            forecate.WindFinder14 = getNopfinderLink(2, addDays);
            forecate.WindFinder17 = getNopfinderLink(3, addDays);
            forecate.WindFinder20 = getNopfinderLink(4, addDays);
            forecate.weatherOnline8 = GetWeatheronline("06", addDays);
            forecate.weatherOnline11 = GetWeatheronline("09", addDays);
            forecate.weatherOnline14 = GetWeatheronline("12", addDays);
            forecate.weatherOnline17 = GetWeatheronline("15", addDays);
            forecate.weatherOnline20 = GetWeatheronline("18", addDays);
            forecate.uoa8 = GetUOA(0, addDays);
            forecate.uoa14 = GetUOA(2, addDays);
            forecate.uoa20 = GetUOA(4, addDays);
            return PartialView(forecate);
        }

        public ActionResult WaveForecast(int? addDays)
        {
            if (addDays == null)
                addDays = 0;

            var model = new ForecastWaveModel();

            if (!IsCurrentUserRegistered())
                model.IsRegistered = false;
            else
                model.IsRegistered = true;

            model.BlockWaveForecast = _windAlertSettings.BlockForecast;
            model.addDays = (int)addDays;
            model.Graph = buildGraph(34.7914, 32.3685);
            int updateHoursAgo = CheckUpadteDate();
            model.Isramar6 = GetIsramarWaves(updateHoursAgo,"06",(int) addDays);
            model.Isramar12 = GetIsramarWaves(updateHoursAgo, "12", (int)addDays);
            model.Isramar18 = GetIsramarWaves(updateHoursAgo,"18", (int)addDays);
            return View(model);
        }

        public ActionResult ForecastUpdated()
        {
            var _forecastSettings = _settingService.LoadSetting<ForecastSettings>(0);  
            if (_forecastSettings.EndDate > DateTime.Now)
                return Content ("/WindAlert/Images/green.png");
            else
                return Content ("/WindAlert/Images/red.png");
        }
    }
}
